<template>
	<view class="content" :style="{ height: windowHeight + 'px', paddingTop: statusBarHeight + 'px' }">
		<view class="top">
			<view class="div">
				<view :style="{ marginLeft: index == 0 ? '0rpx' : '40rpx' }" @click="navClick(item)"
					:class="navIndex == item.value ? 'top_items_active' : 'top_items'" v-for="(item, index) in navList"
					:key="index">
					{{ item.label }}
				</view>
			</view>
		</view>
		<view class="container">
			<scroll-view @scrolltolower="lowerBottom" class="box_scroll" scroll-y="true" refresher-enabled="true"
				:refresher-triggered="triggered" :refresher-threshold="50" refresher-background="#f8f8f8"
				@refresherrefresh="onRefresh" @refresherrestore="onRestore">
				<view class="pictur">
					<view class="left">
						<image class="image"
							src="https://dujiaojing.oss-cn-huhehaote.aliyuncs.com/uploads/2025/4/6/1743939464994_8f2d415e/11.png"
							alt=""></image>
					</view>
					<view class="right">
						<view class="right_top">
							<image class="image"
								src="https://dujiaojing.oss-cn-huhehaote.aliyuncs.com/uploads/2025/4/6/1743939572579_5686172a/22.png"
								alt=""></image>
						</view>
						<view class="right_bottom">
							<image class="image"
								src="https://dujiaojing.oss-cn-huhehaote.aliyuncs.com/uploads/2025/4/6/1743939585492_95a307b9/33.png"
								alt=""></image>
						</view>
					</view>
				</view>
				<view class="contents">
					<view class="titles">科普圈子</view>
					<view class="div" v-for="(item, index) in list" :key="index">
						<image :src="item.avatarImage" alt="" class="touxiang"></image>
						<view class="right">
							<view class="name">{{ item.nickName }}</view>
							<view class="icons">
							</view>
							<view class="font">{{ item.content }}</view>
							<view class="img">
								<image :src="it" alt="" @click="clickImage(item.imageList, it)"
									v-for="(it, dexs) in item.imageList" :key="dexs">
								</image>
							</view>
							<view class="shezhi">
								<view class="date">{{ item.createTime }}</view>
								<view class="shezhi_flex">
									<view class="dianzan">{{ item.hits || 0 }}</view>
									<!-- <view class="heart" @click="shoucang(item)">
										<u-icon name="heart" size="28" v-if="item.collectStatus == '0'"></u-icon>
										<u-icon name="heart-fill" size="28" color="red" v-if="item.collectStatus == 1"></u-icon>
									</view> -->
									<!-- <view class="dian" @click="dianClick(index, item.flag)">
										<u-icon name="more-dot-fill" size="28"></u-icon>
									</view> -->
								</view>
							</view>
							<view class="shezhi shezhis">
								<view class="left">
									<image v-for="(it, ins) in item.touxiang" :key="ins"
										:class="ins == 0 ? 'image' : 'imags'" :src="it" alt="">
									</image>
								</view>
								<view class="right">
									<view class="hits">{{ item.replies.length }}</view>
									<u-icon name="chat" class="chat" size="38"
										@click="huifu(item.nickName, item, 0)"></u-icon>
									<view class="heart" @click="shoucang(item)">
										<u-icon name="heart" size="38" v-if="item.collectStatus == '0'"></u-icon>
										<u-icon name="heart-fill" size="38" color="red"
											v-if="item.collectStatus == 1"></u-icon>
									</view>
									<view class="fold" @click="foldClcik(item.fold, index)">
										<image
											src="https://dujiaojing.oss-cn-huhehaote.aliyuncs.com/uploads/2025/7/11/1752225029365.png?x-oss-process=image/resize,w_300"
											mode=""></image>
									</view>
								</view>
							</view>
							<view class="huifu_people" v-if="!item.fold">
								<view class="huifu_people_items" v-for="(it, inx) in item.replies" :key="inx"
									@click="huifu(it.nickName, item, it.id)">
									<view>{{ it.nickName }}:</view>
									<view class="huifu_contentss">{{ it.replyContent }}</view>
								</view>
							</view>
						</view>
					</view>

					<view class="box_list_noData" v-if="list.length == 0 && !downRefresh">
						<image class="noData_img"
							src="https://dujiaojing.oss-cn-huhehaote.aliyuncs.com/uploads/2025/7/29/1753765605533_4b17a9fa/icon_zanwushuju.png?x-oss-process=image/resize,w_300"
							mode="" />
						<view class="noData_text">
							暂无数据
						</view>
					</view>

					<u-loadmore v-if="list.length != 0" fontSize="25" :status="status" />
				</view>
			</scroll-view>
		</view>




		<view class="fabu" @click="fabu">
			<image class="image"
				src="https://dujiaojing.oss-cn-huhehaote.aliyuncs.com/uploads/2025/4/6/1743939616188_4fc7d342/%E7%BB%84%2036.png"
				alt="">
			</image>
		</view>

		<!-- 回复 -->
		<u-popup :show="activeShow" mode="bottom" :round="30">
			<view class="active_pop">
				<view class="clsoe">
					<u-icon name="close-circle" color="#000" size="50" @click="activeShow = false"></u-icon>
				</view>
				<view class="title">
					回复 @{{ activeName }}
				</view>
				<u--textarea v-model="contents" maxlength="-1" clearable height="160"></u--textarea>
				<view class="buttons" @click="submits">
					回复
				</view>
			</view>
		</u-popup>
		<Tabbar :tabbar-index="tabbarIndex" @updateTabbar="updateTabbar"></Tabbar>
	</view>
</template>

<script>
	import {
		systemInfo
	} from '../../../utils/mixin'
	import config from '@/config/config.js'
	import {
		circleList,
		getCircleByUserId,
		collectAdd,
		collectDelete,
		addCircleReply,
		circleId
	} from '@/api/index.js'
	import Tabbar from "@/components/tabbar/tabbar.vue"
	export default {
		components: {
			Tabbar
		},
		mixins: [systemInfo],
		data() {
			return {
				tabbarIndex: 1,
				navIndex: '0',
				navList: [],
				list: [],
				activeShow: false,
				// 回复人
				activeName: '',
				//回复内容
				contents: '',
				// 选中圈子内容
				activeData: {},
				// 当前评论id
				parentId: '',

				queryParams: {
					pageNum: 1,
					pageSize: 10,
					type: ''
				},
				status: 'loadmore',
				triggered: false,
				downRefresh: false,
				pageCount: 0,
			}
		},
		onLoad() {
			// #ifdef APP-PLUS || H5
			uni.hideTabBar();
			// #endif

			uni.$on('templateClick', (userId) => {
				this.queryParams.pageNum = 1
				this.list = []
				this.shopList()
			});
			// 状态栏高度
			this.getSystemInfo();

			this.queryParams.pageNum = 1
			this.list = []
			this.shopList()

		},
		onShow() {
			// this.downRefresh = true
			// this.onRefresh()
			// this.queryParams.pageNum = 1
			// this.list = []
			// this.shopList()
			this.getNavlist()
		},
		methods: {
			// 收藏
			async shoucang(item) {
				if (item.collectStatus == 1) {
					await collectDelete({
						id: item.id
					})
				} else {
					await collectAdd({
						businessType: item.type,
						businessId: item.id
					})
				}
				uni.$u.toast(item.collectStatus == 1 ? "取消收藏成功" : '收藏成功')
				item.collectStatus = item.collectStatus == 1 ? '0' : '1'
			},
			// 折叠展开
			foldClcik(fold, index) {
				this.$set(this.list[index], 'fold', !fold)
			},
			// 图片点击放大
			clickImage(item, url) {
				let arr = []
				item.forEach((items, index) => {
					if (items) {
						arr.push(items)
					}
				})
				let index = arr.findIndex(value => value == url)
				uni.previewImage({
					current: index,
					urls: arr,
				});
			},
			// 获取头部科普
			async getNavlist() {
				const res = await getCircleByUserId()
				const navList = []
				navList.push({
					label: '全部科普',
					value: '0',
				}, )
				res.msg.split(',').forEach((element, index) => {
					navList.push({
						label: element,
						value: index + 1
					})
				});
				this.navList = navList
			},
			//触底加载
			lowerBottom() {
				if (this.queryParams.pageNum < this.pageCount) {
					if (!this.downRefresh) {
						this.downRefresh = true
						this.status = 'loading';
						this.queryParams.pageNum += 1
						this.shopList()
					}
				}
			},
			onRefresh() {
				this.triggered = true;
				this.downRefresh = true
				this.queryParams.pageNum = 1
				this.list = []
				this.shopList()
			},
			onRestore() {
				this.triggered = false; // 需要重置
			},
			// 列表
			async shopList() {
				const res = await circleList(this.queryParams)
				res.rows.forEach(element => {
					// 默认评论全部折叠
					element.fold = true
					element.avatarImage = config.pic_prefix + element.avatarImage
					const imageList = []
					const image = element.image.split(',')
					image.forEach((item) => {
						imageList.push(config.pic_prefixs + item)
					})
					element.imageList = imageList

					if(element.replies == null){
						element.replies = []
					}
					const touxiang = []
					element.replies.forEach((item) => {
						touxiang.push(config.pic_prefix + item.avatarImage)
					})
					element.touxiang = touxiang.splice(0, 5)

					const replies = [...element.replies];

					// 将 roleName 不是 "学生" 的数据放到数组的最前面
					const filteredReplies = replies.sort((a, b) => {
						// 如果 a.roleName 不是 "学生"，则排到前面
						if (a.roleName !== '学生' && b.roleName === '学生') {
							return -1;
						}
						// 如果 b.roleName 不是 "学生"，则排到前面
						if (a.roleName === '学生' && b.roleName !== '学生') {
							return 1;
						}
						// 其他情况下保持原有顺序
						return 0;
					});

					// 更新 row.replies 为排序后的数据
					element.replies = filteredReplies;
				});
				this.pageCount = res.total
				//停止加载中
				this.triggered = false;
				this.downRefresh = false
				//处理完逻辑停止刷新
				uni.stopPullDownRefresh();
				if (this.list.length >= this.pageCount) {
					this.status = 'nomore';
				} else {
					let arr = [...this.list, ...res.rows]
					this.list = arr
					if (res.rows.length < 10) {
						this.status = 'nomore';
					}
				}
			},





			toPage() {
				uni.navigateTo({
					url: "/subpackage1/homedetail/homedetail"
				})
			},
			updateTabbar(e) {
				this.tabbarIndex = e
			},
			navClick(item) {
				this.navIndex = item.value
				if (item.label == '全部科普') {
					this.queryParams.type = ''
				} else {
					this.queryParams.type = item.label
				}
				this.onRefresh()
			},
			fabu() {
				uni.navigateTo({
					url: "/subpackage2/fabu/fabu"
				})
			},
			// 回复
			huifu(name, item, id) {
				this.activeShow = true
				if (name.includes('@')) {
					this.activeName = name.split('@')[0]
				} else {
					this.activeName = name
				}
				this.activeData = item
				this.parentId = id
			},
			// 回复提交
			async submits() {
				if (this.contents) {
					await addCircleReply({
						circleId: this.activeData.id,
						parentId: this.parentId,
						replyContent: this.contents
					})
					uni.$u.toast('回复成功');
					this.activeShow = false
					this.contents = ''
					const indexs = this.list.findIndex((item) => item.id == this.activeData.id)
					const res = await circleId(this.activeData.id)
					this.$set(this.list[indexs], 'replies', res.data.replies)
				} else {
					uni.$u.toast('请输入回复内容');
				}
			},
			dianzanClick(index, dianzanliang) {
				this.$nextTick(() => {
					this.$set(this.list[index], 'dianzanliang', !dianzanliang)
				})
			},
			dianClick(index, flag) {
				this.$nextTick(() => {
					this.$set(this.list[index], 'flag', !flag)
				})
			}
		}
	}
</script>

<style lang="less" scoped>
	.content {
		width: 100%;
		box-sizing: border-box;
		padding-bottom: 132rpx;
		background-color: rgb(245, 245, 245);
		height: calc(100vh - 132rpx);
		overflow: hidden;

		.top {
			width: 100%;
			height: 100rpx;
			box-sizing: border-box;
			background-color: #fff;
			padding: 20rpx 30rpx;
			overflow: auto;

			.div {
				height: 100%;
				width: max-content;
			}

			.top_items {
				font-size: 30rpx;
				color: rgb(179, 173, 173);
				width: 75px;
				line-height: 60rpx;
				display: inline-block;
			}

			.top_items_active {
				font-size: 34rpx;
				color: #000;
				width: 75px;
				line-height: 60rpx;
				display: inline-block;
			}
		}

		.container {
			width: 100%;
			height: calc(100% - 100rpx);

			.box_scroll {
				height: 100%;
			}
		}

		.pictur {
			width: calc(100% - 60rpx);
			height: 400rpx;
			margin-top: 40rpx;
			background-color: #fff;
			box-sizing: border-box;
			padding: 20rpx 30rpx;
			margin-left: 30rpx;
			display: flex;
			align-items: center;
			justify-content: space-between;
			border-radius: 15px;

			.left {
				width: 45%;
				height: 100%;

				image {
					width: 100%;
					height: 100%;
				}
			}

			.right {
				width: 50%;
				height: 100%;
				display: flex;
				align-items: center;
				flex-wrap: wrap;
				align-content: space-between;

				.right_top {
					height: 45%;
					width: 100%;

					image {
						width: 100%;
						height: 100%;
					}
				}

				.right_bottom {
					height: 45%;
					width: 100%;

					image {
						width: 100%;
						height: 100%;
					}
				}
			}
		}

		.contents {
			width: calc(100% - 60rpx);
			// height: calc(100% - 580rpx);
			background-color: #fff;
			box-sizing: border-box;
			margin-top: 40rpx;
			padding: 20rpx 30rpx;
			margin-left: 30rpx;
			// overflow-y: auto;
			border-radius: 15px;

			.box_scroll {
				height: 100%;
			}

			.box_list_noData {
				display: flex;
				flex-flow: column;
				align-items: center;

				.noData_img {
					width: 80%;
				}

				.noData_text {
					color: #B8BECA;
				}
			}

			.titles {
				font-size: 30rpx;
				color: #000;
				width: 100%;
				height: 50rpx;
			}

			.div {
				margin: 30rpx 0;
				display: flex;
				align-items: flex-start;
				justify-content: space-between;
				width: 100%;

				.touxiang {
					width: 90rpx;
					height: 90rpx;
					margin-right: 10rpx;
					border-radius: 50%;
				}

				.right {
					width: calc(100% - 100rpx);

					.name {
						font-size: 30rpx;
						width: 100%;
					}

					.icons {
						display: flex;
						align-items: center;
						width: 100%;
						margin-top: 10rpx;

						.image {
							width: 20rpx;
							height: 20rpx;
							margin-right: 5rpx;
						}
					}

					.font {
						font-size: 30rpx;
						width: 100%;
						margin-top: 10rpx;
					}

					.img {
						width: 100%;
						display: flex;
						align-items: center;
						flex-wrap: wrap;
						margin-top: 10rpx;
						justify-content: flex-start;

						image {
							width: 23%;
							height: 100rpx;
							margin-right: 10rpx;
						}
					}

					.shezhi {
						margin-top: 10rpx;
						font-size: 20rpx;
						color: rgb(179, 173, 173);
						display: flex;
						width: 100%;
						align-items: center;
						justify-content: space-between;

						.shezhi_flex {
							display: flex;
							align-items: center;

							.heart {
								margin-left: 20rpx;
							}
						}

						.left {
							display: flex;
							align-items: center;

							.image {
								width: 20px;
								height: 20px;
								border-radius: 50%;
							}

							.imags {
								width: 20px;
								height: 20px;
								border-radius: 50%;
								margin-left: -5px;
							}
						}

						.right {
							display: flex;
							align-items: center;
							justify-content: flex-end;

							.hits {
								margin-right: 10rpx;
							}

							.heart {
								margin-left: 10rpx;
							}

							.fold {
								margin-left: 10rpx;
								color: blue;

								image {
									width: 28rpx;
									height: 28rpx;
								}
							}
						}

						.dian {
							color: #000;
							margin-left: 20rpx;
						}
					}

					.shezhis {
						margin-bottom: 10rpx;
					}

					.huifu {
						width: 100%;
						margin-top: 20rpx;

						.huifu_items {
							background-color: rgb(232, 239, 247);
							box-sizing: border-box;
							padding: 10rpx;
							width: 100%;

							.huifu_header {
								display: flex;
								align-items: center;
								justify-content: space-between;

								.huifu_left {
									display: flex;
									align-items: center;

									.huifu_touxiang {
										width: 40rpx;
										height: 40rpx;
										margin-right: 10rpx;
									}

									.huifu_center {
										.huifu_name {
											font-size: 26rpx;
											color: #000;
										}

										.huifu_date {
											color: rgb(179, 173, 173);
											font-size: 20rpx;
										}
									}
								}

								.huifu_dianzanshu {
									display: flex;
									align-items: center;
									color: rgb(179, 173, 173);
									font-size: 20rpx;

									.dianzanshu {
										margin-right: 10rpx;
									}
								}

							}

							.huifu_content {
								width: 100%;
								color: rgb(97, 95, 95);
								font-size: 20rpx;
								margin: 10rpx 0;
								line-height: 1.5;
								letter-spacing: 2rpx;
								box-sizing: border-box;
								padding: 0 10rpx;
							}
						}
					}

					.huifu_dianzan {
						width: 100%;
						display: flex;
						align-items: center;
						justify-content: flex-end;
						margin-top: 10rpx;

						/deep/ .u-icon--right {
							margin-left: 10rpx;
						}
					}

					.huifu_people {
						width: 100%;

						.huifu_people_items {
							display: flex;
							margin-bottom: 15rpx;
							align-items: center;
							font-size: 24rpx;

							.huifu_contentss {
								color: rgb(179, 173, 173);
								font-size: 22rpx;
								margin-left: 10rpx;
							}
						}
					}
				}
			}
		}

		.fabu {
			position: fixed;
			bottom: 200rpx;
			right: 100rpx;
			width: 100rpx;
			height: 100rpx;

			image {
				width: 100%;
				height: 100%;
			}
		}

		.active_pop {
			position: relative;
			width: 100%;
			min-height: 470rpx;
			box-sizing: border-box;
			padding: 20rpx 80rpx;


			.clsoe {
				position: absolute;
				z-index: 2;
				right: 20rpx;
				top: 20rpx;

				image {
					width: 48rpx;
					height: 48rpx;
				}
			}

			.title {
				font-size: 28rpx;
				margin-bottom: 20rpx;
			}

			/deep/ .u-textarea {
				background-color: rgb(245, 247, 250);
			}

			.buttons {
				width: 150rpx;
				height: 60rpx;
				background-color: rgba(34, 175, 229);
				color: #fff;
				line-height: 60rpx;
				text-align: center;
				font-size: 30rpx;
				border-radius: 10rpx;
				letter-spacing: 5rpx;
				margin-top: 40rpx;
			}
		}
	}
</style>